<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9 0009
 * Time: 14:30
 */

namespace app\admin\controller;


use app\model\model\Application_type;
use think\Request;

class Application extends Base
{

    protected $_Application_type;

    protected $_application;
    protected $_ = [];
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_Application_type = new Application_type();

        $this->_application = new \app\model\model\Application();

    }

    public function lists(){
        if(Request::instance()->post()){
            $where = ['flag'=>1];
           $name = input('name');
            $type1 = input('type1');
            $type2 = input('type2');
            $type3 = input('type3');
            if($name){
                $where['name'] = ['like',"%$name%"];
            }
            if($type1){
                $where['type1'] = $type1;
            }
            if($type2){
                $where['type2'] = $type2;
            }
            if($type3){
                $where['type3'] = $type3;
            }
            $data = $this->_application->getList($where, input('limit'));
            foreach ($data as $val){
                $val['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
                $val['Material'] = $val['Industry'] = $val['Material'] = 'Processing';
                if($val['type1']){
                    $val['Material']  = $this->_Application_type->getOne($val['type1'])['name'];
                }
                if($val['type2']){
                    $val['Industry']  = $this->_Application_type->getOne($val['type2'])['name'];
                }
                if($val['type3']){
                    $val['Processing']  = $this->_Application_type->getOne($val['type3'])['name'];
                }

                $val['uname'] = str_replace(" ","-",$val['name']);
            }
            $list = $data->toArray();

            echo json_echo($list);
        } else {
            $app_type_list = $this->_Application_type->getListSelect();
            $this->assign('app_type_list', $app_type_list);
            return $this->fetch();
        }
    }

    public function add(){
        $app_type_list = $this->_Application_type->getListSelect();
        $this->assign('app_type_list', $app_type_list);

        $product = new \app\model\model\Product_type();
        $product_type_list = $product->getListSelect();
        $this->assign('product_type_list', $product_type_list);

        $sample_type_model = new \app\model\model\Sample_type();
        $sample_type_list = $sample_type_model->getListSelect();
        $this->assign('sample_type_list', $sample_type_list);

        $advantage_model = new \app\model\model\Advantage();
        $advantage_type_list = $advantage_model->getListHome('1=1');
        $this->assign('advantage_type_list', $advantage_type_list);

        return $this->fetch();
    }

    public function saveApplication(){
        $data = [];
        $data['type1']   = input('type1', 0);
        $data['type2']   = input('type2', 0);
        $data['type3']   = input('type3', 0);
        $data['name']   = input('name', '');
        $data['title']  = input('title','');
        $data['keyword']  = input('keyword','');
        $data['describe']  = input('describe','');
        $data['sort']  = input('sort',255);
        $data['cover_url']  = input('application_cover','');
        $data['alt']  = input('alt','');
        $data['img_url']  = json_encode((isset($_POST['application_img'])?$_POST['application_img']:array()));
        $data['introduce']  = input('introduce','');
        $scene_tags = isset($_POST['scene_tags'])?$_POST['scene_tags']:array();
        $data['scene_tags'] = implode(',', array_unique(array_filter($scene_tags)));
        $data['scene_img']  = json_encode((isset($_POST['scene_img'])?$_POST['scene_img']:array()));
        $mark_tags = isset($_POST['mark_tags'])?$_POST['mark_tags']:array();
        $data['mark_one'] = json_encode([
            'title'     => input('mark_one_title'),
            'img_url'   => input('mark_one_img'),
            'tags'      => implode(',', array_unique(array_filter($mark_tags)))
        ]);

        $mark_two_tags = isset($_POST['mark_two_tags'])?$_POST['mark_two_tags']:array();
        $data['mark_two'] = json_encode([
            'title'     => input('mark_two_title'),
            'img_url'   => input('mark_two_img'),
            'tags'      => implode(',', array_unique(array_filter($mark_two_tags)))
        ]);
        $data['video'] = input('product_video_url','');
        $data['describe1']  = input('describe1','');
        $data['sample_ids'] = json_encode((isset($_POST['sample_input'])?$_POST['sample_input']:array()));
        $data['significance']  = input('significance','');
        $data['significance_img'] = input('significance_img','');
        $data['advantage_ids'] = json_encode((isset($_POST['advantage_input'])?$_POST['advantage_input']:array()));
        $data['product_ids'] = json_encode((isset($_POST['product_input'])?$_POST['product_input']:array()));
        $data['help']  = input('help','');
        $data['help_img'] = input('help_img','');
        $data['introduce1']  = input('introduce1','');
        $data['create_time'] = time();
        $res = $this->_application->add($data);
        if($res){
            self::_response(200, 'ok');
        } else {
            self::_response(101, '添加失败');
        }
    }

    public function edit(){
        $id = input('id');
        $app_type_list = $this->_Application_type->getListSelect();
        $this->assign('app_type_list', $app_type_list);

        $product = new \app\model\model\Product_type();
        $product_type_list = $product->getListSelect();
        $this->assign('product_type_list', $product_type_list);

        $sample_type_model = new \app\model\model\Sample_type();
        $sample_type_list = $sample_type_model->getListSelect();
        $this->assign('sample_type_list', $sample_type_list);

        $advantage_model = new \app\model\model\Advantage();
        $advantage_type_list = $advantage_model->getListHome('1=1');
        $this->assign('advantage_type_list', $advantage_type_list);

        $data = $this->_application->getOne($id);
        $data['img_url_arr'] = json_decode($data['img_url'], true);
        $data['scene_tags_arr'] = explode(',', $data['scene_tags']);
        $data['scene_img_arr'] = json_decode($data['scene_img'], true);
        $data['mark_one_arr'] = json_decode($data['mark_one'], true);
        $data['mark_two_arr'] = json_decode($data['mark_two'], true);
        $data['advantage_arr'] = json_decode($data['advantage_ids'], true);
        $data['product_ids_arr'] = json_decode($data['product_ids'], true);
        if($data['product_ids_arr']){
            $product_model = new \app\model\model\Product();
            $product_list = $product_model->getListSelect(['id'=>['in', $data['product_ids_arr']]]);
            $data['product_ids_arr'] = $product_list;
        }
        $data['sample_ids_arr'] = json_decode($data['sample_ids'], true);
        if($data['sample_ids_arr']){
            $sample_model = new \app\model\model\Sample();
            $sample_list = $sample_model->getListSelect(['id'=>['in', $data['sample_ids_arr']]]);
            $data['sample_ids_arr'] = $sample_list;
        }
        $this->assign('data', $data);

        return $this->fetch();
    }

    public function updateApplication(){
        $data = [];
        $id   = input('id');
        $data['type1']   = input('type1', 0);
        $data['type2']   = input('type2', 0);
        $data['type3']   = input('type3', 0);
        $data['name']   = input('name', '');
        $data['title']  = input('title','');
        $data['keyword']  = input('keyword','');
        $data['describe']  = input('describe','');
        $data['sort']  = input('sort',255);
        $data['cover_url']  = input('application_cover','');
        $data['alt']  = input('alt','');
        $data['img_url']  = json_encode((isset($_POST['application_img'])?$_POST['application_img']:array()));

        $data['introduce']  = input('introduce','');
        $scene_tags = isset($_POST['scene_tags'])?$_POST['scene_tags']:array();
        $data['scene_tags'] = implode(',', array_unique(array_filter($scene_tags)));
        $data['scene_img']  = json_encode((isset($_POST['scene_img'])?$_POST['scene_img']:array()));
        $mark_tags = isset($_POST['mark_tags'])?$_POST['mark_tags']:array();
        $data['mark_one'] = json_encode([
            'title'     => input('mark_one_title'),
            'img_url'   => input('mark_one_img'),
            'tags'      => implode(',', array_unique(array_filter($mark_tags)))
        ]);

        $mark_two_tags = isset($_POST['mark_two_tags'])?$_POST['mark_two_tags']:array();
        $data['mark_two'] = json_encode([
            'title'     => input('mark_two_title'),
            'img_url'   => input('mark_two_img'),
            'tags'      => implode(',', array_unique(array_filter($mark_two_tags)))
        ]);
        $data['video'] = input('product_video_url','');
        $data['describe1']  = input('describe1','');
        $data['sample_ids'] = json_encode((isset($_POST['sample_input'])?$_POST['sample_input']:array()));
        $data['significance']  = input('significance','');
        $data['significance_img'] = input('significance_img','');
        $data['advantage_ids'] = json_encode((isset($_POST['advantage_input'])?$_POST['advantage_input']:array()));
        $data['product_ids'] = json_encode((isset($_POST['product_input'])?$_POST['product_input']:array()));
        $data['help']  = input('help','');
        $data['help_img'] = input('help_img','');
        $data['introduce1']  = input('introduce1','');

        $res = $this->_application->up(['id'=>$id], $data);
        if($res !==false){
            self::_response(200, 'ok');
        } else {
            self::_response(101, '添加失败');
        }
    }

    public function delApplication(){
        $re = $this->_application->up(['id'=>input('id')], ['flag'=>3]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

    /**
     * 批量删除
     * */
    public function delAllApplication(){
        $re = $this->_application->up(['id'=>['in', input()['data']]], ['flag'=>3]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }

    }

    public function recycled(){
        if(Request::instance()->post()){
            $where = ['flag'=>3];
            $name = input('name');
            if($name){
                $where['name'] = ['like',"%$name%"];
            }
            $data = $this->_application->getList($where, input('limit'));
            foreach ($data as $val){
                $val['create_time'] = date('Y-m-d H:i:s', $val['create_time']);

            }
            $list = $data->toArray();

            echo json_echo($list);
        } else {
            return $this->fetch();
        }
    }

    public function recycledDelApplication(){
        $re = $this->_application->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

    public function recycledDelAllApplication(){
        $re = $this->_application->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

    public function recoveryApplication(){
        $re = $this->_application->up(['id'=>input('id')], ['flag'=>1]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

    public function recoveryallApplication(){
        $re = $this->_application->up(['id'=>['in', input()['data']]], ['flag'=>1]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

    public function getProductList(){
        $category_id = input('category_id');
        if(empty($category_id)){
            self::_response(101, '缺少参数');
        }
        $product_model = new \app\model\model\Product();
        $product_list = $product_model->getListSelect(['type_id'=>$category_id]);
        foreach ($product_list as $val){
            $val['img_url_site'] = json_decode($val['img'], true)[0];
        }
        self::_response(200, 'ok', $product_list);
    }

    public static function  _response($status = 101, $msg = '', $data = []){
        header('Content-type:application/json;charset=utf-8');
        $response = ['code' => intval($status), 'msg' => $msg];
        $response['data'] = $data ? $data : [];
        exit(json_encode($response, JSON_UNESCAPED_UNICODE));
    }

    /**
     * 排序
     * */
    public function sort(){
        $data = ['sort'=>input('sort')];
        $re = $this->_application->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
    /*=======应用=======*/
    public function typelists(){
        return $this->fetch();
    }
    public function getListType(){
        $where = [];
        $name = input('name');
        if($name){
            $where['name'] = ['like',"%$name%"];
        }
        $data = $this->_Application_type->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            if($v['type']==1){
                $v['type']='Material';
            }elseif ($v['type']==2){
                $v['type']='Industry';
            }elseif ($v['type']==3){
                $v['type']='Processing';
            }
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function typeadd(){
        if(!Request::instance()->post()){
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
                'type'=>input('type'),
                'content'=>input('editorValue'),
                'create_time'=>time(),
            ];
            $re = $this->_Application_type->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function typeup(){
        if(!Request::instance()->post()){
            $data = $this->_Application_type->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
                'type'=>input('type'),
                'content'=>input('editorValue'),
            ];
            $re = $this->_Application_type->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOneType(){
        $data = $this->_Application_type->getOne(input('id'));
        if($data['type']==1){
            $data['type']='Trotec';
        }elseif ($data['type']==2){
            $data['type']='Industry';
        }elseif ($data['type']==3){
            $data['type']='Processing';
        }
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function delType(){

        $re = $this->_Application_type->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAllType(){
        $re = $this->_Application_type->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 排序
     * */
    public function typesort(){
        $data = ['sort'=>input('sort')];
        $re = $this->_Application_type->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
}